import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Tester {

	/**
	 * @param args
	 */
	

	public static ArrayList<String> preprocessTestDoc(String testDocPath, String stopwordSource) {

		HashMap<Integer, DocumentEntity> parsedTestDoc = new HashMap<Integer, DocumentEntity>();
		ArrayList<String> tokensInTestBody = new ArrayList<String>();
		DocumentEntity testDoc;
		String testBody = null, modBody;
		StopWordRemoveResult swr;

		TestDocParser testDocParser = new TestDocParser();

		parsedTestDoc = testDocParser.parseDocument(testDocPath);
		System.out.println("Parsed map:" + parsedTestDoc.toString());

		StopwordRemover stopwordRemover = new StopwordRemover();
		Set<Map.Entry<Integer, DocumentEntity>> set = parsedTestDoc.entrySet();

		testDoc = new DocumentEntity();

		for (Map.Entry<Integer, DocumentEntity> me : set) {
			testDoc = me.getValue();

			if (testDoc != null)
				testBody = testDoc.getBody();

			if (testBody != null) {

				swr = stopwordRemover.remove(stopwordSource, testBody);
				modBody = swr.getModifiedBody();
				// replace the body of the article with the stopwords removed
				testDoc.setBody(modBody);
				tokensInTestBody = swr.getVocabulary();
				System.out.println("stop word removed:" + modBody);
				System.out.println("tokens in test body:"
						+ tokensInTestBody.toString());

			}
		}
		
		return tokensInTestBody;
	}

	public static void computeScore(HashMap<String, ClassDetails> classInfoMap, ArrayList<String> tokensInTestBody)
	{
		ClassDetails cd;
		String currentClass, currentToken;
		double currentScore = 0.0, tempConditionalProb;
		HashMap<String, Double> scoreMap = new HashMap<String, Double>(); 
		HashMap<String, Double> conditionalProbs = new HashMap<String, Double>();
		
		
		Set<Map.Entry<String, ClassDetails>> set = classInfoMap.entrySet();
		
		//iterate over the entire hashmap containing all details of all classes
		for (Map.Entry<String, ClassDetails> me : set) {
			cd = new ClassDetails();
			cd = me.getValue();
			currentClass = me.getKey();
			System.out.println("CurrentClass:"+currentClass);
			System.out.println("PriorProb:"+cd.getPriorProbOfClass());
			currentScore = Math.log(cd.getPriorProbOfClass());
			
			Iterator<String> itr = tokensInTestBody.iterator();
			while (itr.hasNext())
			{
				currentToken = itr.next();
			
				conditionalProbs = cd.getConditionalProbs();
				tempConditionalProb = conditionalProbs.get(currentToken);
				currentScore+=Math.log(tempConditionalProb);
			}
			scoreMap.put(currentClass, currentScore);
			
		}
		for (Iterator i = sortByValue(scoreMap).iterator(); i.hasNext(); ) {
            String key = (String) i.next();
            System.out.printf("key: %s, value: %s\n", key, scoreMap.get(key));
        }
    
	}
	
	
	public static ArrayList<String> sortByValue(final HashMap<String, Double> m) {
        ArrayList<String> keys = new ArrayList<String>();
        keys.addAll(m.keySet());
        Collections.sort(keys, new Comparator() {
            public int compare(Object o1, Object o2) {
                Object v1 = m.get(o1);
                Object v2 = m.get(o2);
                if (v1 == null) {
                    return (v2 == null) ? 0 : 1;
                }
                else if (v1 instanceof Comparable) {
                    return ((Comparable) v1).compareTo(v2);
                }
                else {
                    return 0;
                }
            }
        });
        return keys;
    }
}
